<?php
$version = Zend_Registry::get('version');
?>

<script type="text/javascript">
$(document).ready(function() {	
	floatingbar('#floatingbar',"#button_top_bar",null);
	
	function openMsgDialog(msg)
	{
		$('#dialog_msg').html(msg).dialog({
			autoOpen: true,
			modal: true,
			show: 'fade',
			resizable: 'false',				
			buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, "UTF-8"); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
			open: function(event, ui)
				 {
					setTimeout(function() 
					{									
						if($('#' + event.target.id).dialog('isOpen'))
						{
							$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
						}
					}, 10000);
				  }
		});
	}
	
		
	$('a.reset_btn').click(function() {			
		 resetFields();	
		 removeError();	
	});
	
	function resetFields()
	{
		$( "form" )[ 0 ].reset();			
	}
	
	function removeError()
	{
		$('#actionMessage').html('');
		
		var filename = '';
		var filevalue = '';		
		$.each($('form').serializeArray(), function(i, field) {	
				if(field.name.match(/\[\]$/))
				{
					var filearr = field.name.split('[]');
					filename = filearr[0];					
				}
				else
				{
					filename 	= 	field.name;
				} 			
				$('#'+filename).removeClass('ui-state-error');
				$('#'+filename+'_err').html('');									
		});				
	}		
	
	function addError(key,errMsg)
	{
		var err_id = key+'_err';
		var field_name = key;
		$('#'+field_name).addClass('ui-state-error');
		$('#'+err_id).append(errMsg);
	}
	
	function openLoaderDialog()
	{
		$('#dialog_container').dialog({
					autoOpen: true,
					modal: true,
					show: 'fade',
					resizable: 'false',				
					open: function(event, ui)
						 {
						 	$("#progressbar").progressbar({
								value:100
				
							});							
						  }
				});
	}
	
	function closeLoaderDialog()
	{
		$('#dialog_container').dialog('option','hide','explode').dialog('close');
	}
	
	function getFormData()
	{
		var fieldObj = {};
		var filename = '';
		var filevalue = '';
		$.each($('form').serializeArray(), function(i, field) {	
			if(field.name.match(/\[\]$/))
			{
				var filearr = field.name.split('[]');
				filename = filearr[0];
				filevalue = $('#'+filename).val();
			}
			else
			{
				filename 	= 	field.name;
				filevalue 	= 	field.value;
			}			
			fieldObj[filename] = filevalue;			
		});
		return fieldObj;
	}
	
	$('a.edit_setting_btn').click(function() {			
			
			var formData = getFormData();	
			formData.id = '<?php echo $this->id; ?>';				
		$.ajax({
				url: "<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'edit' ), 'adminrout',    true) ?>",
			  	type: 'POST',
				beforeSend: function(){						
						removeError();
						openLoaderDialog();				
					 },
			  	data: formData,
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");					
					closeLoaderDialog();					
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));													
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
				},
				error: function(xhr, status, error){
					closeLoaderDialog();
					var msg = "Error! " + xhr.status + " " + error;
					$('#actionMessage').html(errMsgDesign(msg));
				}
		});
	});
	
	$('select.country_id').change(function() {	
			if($('select.country_id option:selected').val() == 'any' || $('select.country_id option:selected').val() =='')
			{
				$('select.city_id').html('<option value=""><?php echo $this->translator->translator("common_select"); ?></option>');	
				$('select.state_id').html('<option value=""><?php echo $this->translator->translator("common_select_state"); ?></option>');
			}
			else
			{	
				$.ajax({
					  url: "<?php echo $this->url( array('module' =>'Invoice','controller' => 'setting', 'action'     => 'states' ), 'adminrout',    true) ?>",
					  type: 'POST',
					  data: { id: $('select.country_id option:selected').val() },
					  beforeSend: function(){						
								$('select.state_id').html('<option value=""><?php echo $this->translator->translator("common_loading_language"); ?></option>');
								$('select.city_id').html('<option value=""><?php echo $this->translator->translator("common_select"); ?></option>');			
								$('#state_id_err').html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" height="16" border="0" />');	
							 },
					  success: function(response){
						//alert(response);
						var json_arr = eval("("+response+")");
						var option = '<option value=""><?php echo $this->translator->translator("common_select_state"); ?></option>';				
						if(json_arr.status == 'ok')
						{
							for(var i = 0;i<json_arr.states.length;i++)
							{
								option += '<option value="'+json_arr.states[i].state_id+'" >'+stripslashes(json_arr.states[i].state_name)+'</option>';
							}
							$('select.state_id').html(option);
						}
						else
						{
							$('select.state_id').html('<option value="">'+json_arr.msg+'</option>');
						}
						$('#state_id_err').html('');
					  }
				});
			}
		});
		
		$('select.state_id').change(function() {		
					
			if($('select.state_id option:selected').val() == 'any' || $('select.state_id option:selected').val() == '')
			{
				$('select.city_id').html('<option value=""><?php echo $this->translator->translator("common_select"); ?></option>');
			}
			else
			{
				$.ajax({
					  url: "<?php echo $this->url( array('module' => 'Invoice', 'controller' => 'setting', 'action'     => 'areas' ), 'adminrout',    true) ?>",
					  type: 'POST',
					  data: { id: $('select.state_id option:selected').val() },
					  beforeSend: function(){						
							$('select.city_id').html('<option value=""><?php echo $this->translator->translator("common_loading_language"); ?></option>');
							$('#city_id_err').html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" height="16" border="0" />');	
							},
					  success: function(response){						
						var json_arr = eval("("+response+")");
						var option = '<option value=""><?php echo $this->translator->translator("common_select"); ?></option>';				
						if(json_arr.status == 'ok')
						{
							for(var i = 0;i<json_arr.cities.length;i++)
							{
								option += '<option value="'+json_arr.cities[i].city_id+'" >'+stripslashes(json_arr.cities[i].city)+'</option>';
							}
							$('select.city_id').html(option);
						}
						else
						{
							$('select.city_id').html('<option value="">'+json_arr.msg+'</option>');
						}
						$('#city_id_err').html('');
					  }
				});
			}
		});
	
});
function stripslashes(str) 
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}
</script>

<div id="container">
<?php if (!$this->allow()) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>

<div id="button_top_bar"></div>
<div class="title-container">
	<div class="invoice-template-edit"><h1><?php echo $this->translator->translator('invoice_setting_edit_page_name'); ?></h1></div>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'list' ), 'adminrout',    true) ?>" class="back_btn_link"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="edit_setting_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
    </div>
</div>

<div class="content-container">
	<div id="actionMessage"></div>
	<form name="formCategory" id="formCategory" method="post">
        <div id="generalInfo">
        	<fieldset class="fieldset last">
                <legend class="legend"><?php echo $this->translator->translator('invoice_setting_add_page_info'); ?></legend>
                <div class="form">
                	<?php												
						$elements = $this->settingForm->getElements();
						foreach($elements as $element)
						{
							if($element->getName() == 'setting_logo')
							{
					?>
                    <div class="form-row">
                        <div class="label">
                            <?php echo $element->getLabel();  if($element->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                        	<div class="upload-img">
                            	<span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="upload_btn"><span class="icon upload"><?php echo $this->translator->translator('common_upload'); ?></span></a></span>
                                <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($element->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            </div>
                            
                            <?php echo $element; ?>&nbsp;&nbsp;<span id="selected_file"><?php echo $assign_image; ?></span>
                            <div class="input-errors" id="<?php echo $element->getName().'_err'; ?>"></div>
                        </div>
                    </div>
                    <?php
							}								
							else if($element->getName() == 'primary_image')
							{
					?>
                    <div class="form-row">
                        <div class="label">
                        </div>
                        
                        <div class="field">
                            <div class="input-errors" id="<?php echo $element->getName().'_err'; ?>"></div>
                        </div>
                 	</div>
                    <?php
							}
							else
							{
					?>
                    <div class="form-row">
                        <div class="label">
                        	<?php echo $element->getLabel();  if($element->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $element; if($element->getName() == 'basic_price' || $element->getName() == 'descount_price') { echo $currency->getSymbol(); }?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($element->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="<?php echo $element->getName().'_err'; ?>"></div>
                        </div>
                 	</div>
                    <?php
							}
						}
					?>
             	</div>
         	</fieldset>
        </div>
  	</form>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'list' ), 'adminrout',    true) ?>" class="back_btn_link"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="edit_setting_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
    </div>
</div>

<div id="floatingbar">
	<h1><?php echo $this->translator->translator('invoice_setting_edit_page_name'); ?></h1>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'list' ), 'adminrout',    true) ?>" class="back_btn_link"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="edit_setting_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
    </div>
</div>
<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('invoice_setting_edit_page_name')); ?>"></div>
<div id="dialog_container" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
	<div id="progressbar" ></div>	
</div>
<?php endif; ?>
</div>